package JDBC_student_table;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: yang
 * Date: 2022-09-01
 * Time: 19:05
 */
public class JDBC_SEARCH {
    public static void main(String[] args) throws SQLException {
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/bite?characterEncoding=UTF-8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("1100ppqqaall");

        Connection connection = dataSource.getConnection();
        String sql = "select * from student where id = ?;";
        PreparedStatement statement = connection.prepareStatement(sql);
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入你要查询的学生id:");
        int id = scan.nextInt();
        statement.setInt(1,id);
        ResultSet resultSet = statement.executeQuery();
        String name;
        while(resultSet.next()){
            id = resultSet.getInt("id");
            name = resultSet.getString("name");
            System.out.println("您查询的学生id为 "+id+"  姓名为 "+name);
        }
    }
}
